package com.annotation.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;

/**
 * 计算器切面类
 * @author Lee
 */
@Aspect
public class LogCalculator {

    @Pointcut("execution(* com.annotation.aop.Calculator.*(..))")
    public void pointCut(){}

    @Before(value = "pointCut()")
    public void logStart(JoinPoint joinPoint){
        System.out.println("运行"+ joinPoint.getSignature().getName() +"结束，参数列表是" + Arrays.asList(joinPoint.getArgs()));
    }

    @After("com.annotation.aop.LogCalculator.pointCut()")
    public void logEnd(JoinPoint joinPoint){
        System.out.println("运行"+ joinPoint.getSignature().getName() + "结束");
    }

    @AfterReturning(value = "pointCut()",returning = "result")
    public void logReturning(JoinPoint joinPoint,Object result){
        System.out.println("运行" + joinPoint.getSignature().getName() + "结束,返回值为" + result);
    }

    /**
     * JoinPoint必须出现在参数的第一位
     * @param joinpoint
     * @param e
     */
    @AfterThrowing(value = "pointCut()",throwing = "e")
    public void logThrowing(JoinPoint joinpoint, Exception e){
        System.out.println("运行" + joinpoint.getSignature().getName() + "时-->" + e.getMessage());

    }

}
